package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.AnswerDto;
import com.jyy.prefabricated.entity.Exam;
import com.jyy.prefabricated.entity.Student;
import com.jyy.prefabricated.entity.StudentExam;
import com.jyy.prefabricated.enums.ExamStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.service.ExamService;
import com.jyy.prefabricated.service.StudentExamQuestionService;
import com.jyy.prefabricated.service.StudentExamService;
import com.jyy.prefabricated.utils.DateUtils;
import com.jyy.prefabricated.utils.UserUtils;
import com.jyy.prefabricated.vo.StudentExamPaperVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 学员考试题目
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class StudentExamQuestionController {

    @Autowired
    private StudentExamQuestionService studentExamQuestionService;

    @Autowired
    private StudentExamService studentExamService;

    @Autowired
    private ExamService examService;

    @Autowired
    private UserUtils userUtils;

    /**
     * 答题
     * @param dto
     * @return
     */
    @PutMapping("/student-exam-question")
    public AjaxResult answer(@Valid @RequestBody AnswerDto dto){
        log.info("Rest request to update studentExamQuestion:{}",dto);
        //校验当前学生是否匹配
        Student student = userUtils.getCurrentStudent();
        StudentExam studentExam = studentExamService.selectById(dto.getStudentExamId());
        Exam exam = examService.selectById(studentExam.getExamId());
        if(studentExam == null ){
            throw new CustomException("查询不到当前考试信息");
        }

        if(!studentExam.getStudentId().equals(student.getId())){
            throw new CustomException("当前用户和考试信息不匹配");
        }
        if(ExamStatusEnum.BEFORE.getValue().equals(exam.getExamStatus())){
            throw new CustomException("当前考试尚未开始");
        }
        if(ExamStatusEnum.AFTER.getValue().equals(exam.getExamStatus())){
            throw new CustomException("当前考试已结束");
        }
        if(ExamStatusEnum.AFTER.getValue().equals(studentExam.getStatus())){
            throw new CustomException("当前考试已交卷");
        }
        //判断考试是否超时，获取考试的秒数
        Long useTime = DateUtils.pastMinutes(studentExam.getCreateTime());
        Long answerTime = DateUtils.parseDate(exam.getExamEndTime()).getTime() -  DateUtils.parseDate(exam.getExamStartTime()).getTime();

        if(useTime > answerTime){
            throw new CustomException("当前考试答题时间已超时，请立即交卷");
        }
        StudentExamPaperVo result = studentExamQuestionService.answer(dto);
        return AjaxResult.success(result);

    }

    /**
     * 获取考卷
     *
     * @param studentExamId 学生考试id
     * @return
     */
    @GetMapping("/student-exam-question/{studentExamId}")
    public AjaxResult selectById(@PathVariable Integer studentExamId) {
        //校验当前学生是否匹配
        Student student = userUtils.getCurrentStudent();
        log.info("Rest request to find studentExamQuestion by studentExamId:{},studentId:{}", studentExamId, student.getId());
        StudentExam studentExam = studentExamService.selectById(studentExamId);
        if (studentExam == null) {
            throw new CustomException("查询不到当前考试信息");
        }
        Exam exam = examService.selectById(studentExam.getExamId());
        if (!studentExam.getStudentId().equals(student.getId())) {
            throw new CustomException("当前用户和考试信息不匹配");
        }
        if (ExamStatusEnum.BEFORE.getValue().equals(exam.getExamStatus())) {
            throw new CustomException("当前考试尚未开始");
        }
        if (ExamStatusEnum.AFTER.getValue().equals(exam.getExamStatus())) {
            throw new CustomException("当前考试已结束");
        }
        if (ExamStatusEnum.AFTER.getValue().equals(studentExam.getStatus())) {
            throw new CustomException("当前考试已交卷");
        }
        StudentExamPaperVo result = studentExamQuestionService.fetchQuestionsByStudentExamId(studentExamId);
        return AjaxResult.success(result);
    }
}
